<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>localStorage</title>
</head>
<body>
    <button onclick="saveData()">点我保存一个数据</button>
    <button onclick="getData()">点我读取一个数据</button>
    <button onclick="delData()">点我删除一个数据</button>
    <button onclick="delAllData()">点我清除数据</button>
    
    <script>
        let p = {name:'彼暗',age : 18}
        function saveData(){
            window.localStorage.setItem('msg','hello');
            window.localStorage.setItem('msg2',666);
            window.localStorage.setItem('person',JSON.stringify(p));
        }
        function getData(){
            // 没有则为null
            console.log(window.localStorage.getItem('msg'));
            console.log(window.localStorage.getItem('msg2'));
            const p = localStorage.getItem('person');
            console.log(JSON.parse(p));
        }
        
        function delData(){
            window.localStorage.removeItem('msg');
            window.localStorage.removeItem('msg2');
            window.localStorage.removeItem('person');
        }

        function delAllData(){
            // 清除所有
            window.localStorage.clear();
        }
    </script>
</body>
</html>